<!DOCTYPE html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>
    冒泡排序 &amp; 快速排序 · chanchifeng |
    
    ZhiYinQ</title>
  
    <link rel="shortcut icon" href="/favicon.icon">
  
  <link rel="stylesheet" href="/css/style.css">
  
    <link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
  
  <script src="/js/pace.min.js"></script>
</head>
</html>
<link href="https://cdn.staticfile.org/animate.css/3.7.2/animate.min.css" rel="stylesheet">

<body>
<main class="content">
  <section class="outer">
  <style>
.pag a:link, .pag a:visited {
    opacity: 1;
    -webkit-transition: all .15s linear;
    -moz-transition: all .15s linear;
    -o-transition: all .15s linear;
    -ms-transition: all .15s linear;
    transition: all .15s linear;
    color: #424242;
}
.pag a:hover {
    color: #ffffff;
}
.pag :hover {
    background-color: #127ea9;
}
</style>

<article id="post-冒泡排序-快速排序-·-chanchifeng" class="article article-type-post" itemscope itemprop="blogPost" data-scroll-reveal>
  
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      冒泡排序 &amp; 快速排序 · chanchifeng
    </h1>
  
  




      </header>
    

    
      <div class="article-meta">
        <a href="/2019/10/16/冒泡排序-快速排序-·-chanchifeng/" class="article-date">
  <time datetime="2019-10-16T09:14:46.000Z" itemprop="datePublished">2019-10-16</time>
</a>
        
		&emsp;<i class="fe fe-bar-chart"></i> <span class="post-count">675</span>字
		&emsp;<i class="fe fe-clock"></i> <span class="post-count">3</span>分钟
      </div>
    
    
      
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      
      
      
        <p>冒泡排序 &amp; 快速排序 · chanchifeng</p>
<p><img src="/images/logo@2x.png" alt></p>
<a id="more"></a>
<h3 id="chanchifeng"><a href="#chanchifeng" class="headerlink" title="chanchifeng"></a><a href="/">chanchifeng</a></h3><p>Carpet Diem</p>
<ul>
<li><a href="/">首页</a></li>
<li><a href="/about">关于</a></li>
<li><a href="/archives">归档</a></li>
<li><a href="/links">友链</a></li>
<li><a href="/readme">引导</a></li>
</ul>
<p><img src="/images/favicon.png" alt></p>
<h3 id="冒泡排序-amp-快速排序"><a href="#冒泡排序-amp-快速排序" class="headerlink" title="冒泡排序 &amp; 快速排序"></a>冒泡排序 &amp; 快速排序</h3><h3 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a><a href="#冒泡排序" title="冒泡排序"></a>冒泡排序</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">	<span class="keyword">var</span> gulp = <span class="built_in">require</span>(<span class="string">'gulp'</span>);</span><br><span class="line">	<span class="comment">//Plugins模块获取</span></span><br><span class="line">	<span class="keyword">var</span> minifycss = <span class="built_in">require</span>(<span class="string">'gulp-minify-css'</span>);</span><br><span class="line">	<span class="keyword">var</span> uglify = <span class="built_in">require</span>(<span class="string">'gulp-uglify'</span>);</span><br><span class="line">	<span class="keyword">var</span> htmlmin = <span class="built_in">require</span>(<span class="string">'gulp-htmlmin'</span>);</span><br><span class="line">	<span class="keyword">var</span> htmlclean = <span class="built_in">require</span>(<span class="string">'gulp-htmlclean'</span>);</span><br><span class="line">	<span class="comment">//压缩css</span></span><br><span class="line">	<span class="number">1.</span> gulp.task(<span class="string">'minify-css'</span>,</span><br><span class="line">gulp.task(<span class="string">'minify-css'</span>,</span><br><span class="line"><span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> gulp.src(<span class="string">'./public/**/*.css'</span>).pipe(minifycss()).pipe(gulp.dest(<span class="string">'./public'</span>));</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//压缩html</span></span><br><span class="line">gulp.task(<span class="string">'minify-html'</span>,</span><br><span class="line"><span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> gulp.src(<span class="string">'./public/**/*.html'</span>).pipe(htmlclean()).pipe(htmlmin(&#123;</span><br><span class="line">        removeComments: <span class="literal">true</span>,</span><br><span class="line">        minifyJS: <span class="literal">true</span>,</span><br><span class="line">        minifyCSS: <span class="literal">true</span>,</span><br><span class="line">        minifyURLs: <span class="literal">true</span>,</span><br><span class="line">    &#125;))</span><br><span class="line">    .pipe(gulp.dest(<span class="string">'./public'</span>))</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//压缩js 不压缩min.js</span></span><br><span class="line">gulp.task(<span class="string">'minify-js'</span>,</span><br><span class="line"><span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> gulp.src([<span class="string">'./public/**/*.js'</span>, <span class="string">'!./public/**/*.min.js'</span>]).pipe(uglify()).pipe(gulp.dest(<span class="string">'./public'</span>));</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//4.0以前的写法 </span></span><br><span class="line"><span class="comment">//gulp.task('default', [</span></span><br><span class="line"><span class="comment">//  'minify-html', 'minify-css', 'minify-js'</span></span><br><span class="line"><span class="comment">//]);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//4.0以后的写法</span></span><br><span class="line"><span class="comment">// 执行 gulp 命令时执行的任务</span></span><br><span class="line"><span class="comment">//gulp.task('default', gulp.parallel('minify-html', 'minify-css', 'minify-js',</span></span><br><span class="line"><span class="comment">//function() &#123;</span></span><br><span class="line">    <span class="comment">// Do something after a, b, and c are finished.</span></span><br><span class="line"><span class="comment">//	console.log('success')</span></span><br><span class="line"><span class="comment">//&#125;));</span></span><br><span class="line"></span><br><span class="line">gulp.task(<span class="string">'default'</span>,gulp.series(gulp.parallel(<span class="string">'minify-html'</span>,<span class="string">'minify-css'</span>,<span class="string">'minify-js'</span>)));<span class="string">`</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String\[\] args)</span> </span>&#123;  </span><br><span class="line"> <span class="keyword">int</span>\[\] array = <span class="keyword">new</span> <span class="keyword">int</span>\[\]&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">5</span>&#125;;  </span><br><span class="line"> Arrays.stream(bubbleSort(array)).forEach(System.out::println);  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span>\[\] bubbleSort(<span class="keyword">int</span>\[\] array)&#123;  </span><br><span class="line"> <span class="keyword">if</span>(array == <span class="keyword">null</span>)&#123;  </span><br><span class="line"> <span class="keyword">return</span> array;  </span><br><span class="line"> &#125;  </span><br><span class="line"> <span class="keyword">int</span> temp;  </span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; array.length - <span class="number">1</span>; i++) &#123;  </span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; array.length - <span class="number">1</span> \- i; j++) &#123;  </span><br><span class="line"> <span class="keyword">if</span>(array\[j\] &gt; array\[j + <span class="number">1</span>\])&#123;  </span><br><span class="line"><span class="comment">//                    两两交互，正常编写  </span></span><br><span class="line"><span class="comment">//                    temp = array\[j\];  </span></span><br><span class="line"><span class="comment">//                    array\[j\] = array\[j+1\];  </span></span><br><span class="line"><span class="comment">//                    array\[j + 1\] = temp;  </span></span><br><span class="line">  </span><br><span class="line"><span class="comment">//                    两两交互，使用异或  </span></span><br><span class="line"> array\[j + <span class="number">1</span>\] = array\[j + <span class="number">1</span>\] ^ array\[j\];  </span><br><span class="line"> array\[j\] = array\[j + <span class="number">1</span>\] ^ array\[j\];  </span><br><span class="line"> array\[j + <span class="number">1</span>\] = array\[j + <span class="number">1</span>\] ^ array\[j\];  </span><br><span class="line"> &#125;  </span><br><span class="line"> &#125;  </span><br><span class="line"> &#125;  </span><br><span class="line"> <span class="keyword">return</span> array;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="/images/1.png" alt></p>
<blockquote>
<p>对应异或的理解，可以参考李梵博客园的理解 java 使用 异或 交换两数<a href="https://www.cnblogs.com/bg7c/p/7998935.html" target="_blank" rel="noopener">https://www.cnblogs.com/bg7c/p/7998935.html</a></p>
</blockquote>
<h3 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a><a href="#快速排序" title="快速排序"></a>快速排序</h3><p>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br>40<br>41<br>42<br>43<br>44<br>45<br>46  </p>
<pre><code> private static void swap(int\[\] data, int i, int j) {  
//        int temp = data\[i\];  
//        data\[i\] = data\[j\];  
//        data\[j\] = temp;  

 data\[i\] = data\[i\] ^ data\[j\];  
 data\[j\] = data\[i\] ^ data\[j\];  
 data\[i\] = data\[i\] ^ data\[j\];  

 }  

 private static void subSort(int\[\] data, int start, int end) {  
 if (start &lt; end) {  
 int base = data\[start\];  
 int low = start;  
 int high = end + 1;  
 while (true) {  
 while (low &lt; end &amp;&amp; data\[++low\] - base &lt;= 0) {  
 }  
 while (high &gt; start &amp;&amp; data\[--high\] - base &gt;= 0) {  
 }  
 if (low &lt; high) {  
 swap(data, low, high);  
 } else {  
 break;  
 }  
 }  
 swap(data, start, high);  

 subSort(data, start, high - 1);//递归调用  
 subSort(data, high + 1, end);  
 }  
 }  

 public static void quickSort(int\[\] data) {  
 //后面哨兵先走一步  
 subSort(data, 0, data.length - 1);  
 }  


 public static void main(String\[\] args) {  
//        int\[\] data = {10,7,2,4,7,62,3,4,2,1,8,9,19};  
 int\[\] data = {10,7,2,4,7,62};  
 quickSort(data);  
 Arrays.stream(data).forEach(System.out::println);  
 }  </code></pre><blockquote>
<p>理解参考脚丫先生的CSDN的快速排序（java实现）<a href="https://blog.csdn.net/shujuelin/article/details/82423852" target="_blank" rel="noopener">https://blog.csdn.net/shujuelin/article/details/82423852</a></p>
</blockquote>
<pre><code>&lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/gitalk/dist/gitalk.css&quot;&gt;
&lt;script src=&quot;https://unpkg.com/gitalk/dist/gitalk.min.js&quot;&gt;&lt;/script&gt;</code></pre><p>2019-06-07</p>
<p><a href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="noopener"><img src="/images/88x31.png" alt></a></p>
<p>本作品采用<a href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="noopener">知识共享署名-相同方式共享 4.0 国际许可协议</a>进行许可。 本文总阅读量：</p>
<ul>
<li><a href="/2019/06/10/java-basic/" title="Java - 基础知识点">上一篇</a></li>
<li><a href="/2019/06/07/algorithm-binary-search/" title="二分查找">下一篇</a></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <!--<a data-url="https://github3982.github.io/2019/10/16/冒泡排序-快速排序-·-chanchifeng/" data-id="ck1vxnbrv002miwq0eicc67an"
         class="article-share-link">分享</a>“”“分享按钮”“”-->
      
	  
	<div style="text-align:center;color: #ccc;font-size:14px;">------------- 本文结束&nbsp;<i class="fe fe-smile"></i>&nbsp;感谢您的阅读 -------------</div>

    </footer>

  </div>

  
    
  <nav class="article-nav">
    
    
      <a href="/2019/08/15/md - 副本 (4)/" class="article-nav-link">
        <strong class="article-nav-caption">后一篇</strong>
        <div class="article-nav-title">Markdown 基本语法在 Ocean 中的展现形式</div>
      </a>
    
  </nav>


  

  
    

  

</article>



</section>
  <footer class="footer">
  <div class="outer">
    <div class="float-right">
      <ul class="list-inline">
  
    <!--<li><i class="fe fe-smile-alt"></i> <span id="busuanzi_value_site_uv"></span></li>-->
  
</ul>
    </div>
    <ul class="list-inline">
      <li>&copy; 2019 ZhiYinQ</li>
      <li>Powered by <a style="text-decoration: none !important;">Hexo</a></li>
      <li>Theme  <a style="text-decoration: none !important;">Ocean</a></li>
    </ul>
  </div>
</footer>

</main>

<aside class="sidebar sidebar-specter">
  
    <button class="navbar-toggle"></button>
<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img id="img" src="/images/hexo.svg" alt="ZhiYinQ"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">主页</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/dynamic">动态</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/gallery">相册</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/share">分享</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/playlist">播放</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">关于</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="搜索">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      <div class="totop" id="totop">
  <i class="fe fe-rocket"></i>
</div>
    </li>
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
  </aside>
  <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/jquery.justifiedGallery.min.js"></script>
<script src="/js/lazyload.min.js"></script>
<script src="/js/busuanzi-2.3.pure.min.js"></script>

  <script src="/fancybox/jquery.fancybox.min.js"></script>



  <script src="/js/tocbot.min.js"></script>
  <script>
    // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
    tocbot.init({
      tocSelector: '.tocbot',
      contentSelector: '.article-entry',
      headingSelector: 'h1, h2, h3, h4, h5, h6',
      hasInnerContainers: true,
      scrollSmooth: true,
      positionFixedSelector: '.tocbot',
      positionFixedClass: 'is-position-fixed',
      fixedSidebarOffset: 'auto',
    });
  </script>



<script>
// 使用fancybox来显示post图片集(#andus-head-img为头像id)
$('img:not(#img)').each(function() {
  $(this).wrap('<a class="fancybox" data-fancybox="gallery" href="' + $(this).prop("src") + '"></a>'); 
})
</script>


<script src="/js/ocean.js"></script>

</body>
</html>
<script type="text/javascript" src="../dist/music.js"></script>
<script src="js/scrollReveal.js"></script>
</script>

	  <script>
			  $(function(){
    $('div img li').addClass('wdata-scroll-reveal');
});
</script>
<script>
window.scrollReveal = new scrollReveal();
//或者，elem 为动画元素的任何级别的父元素
window.scrollReveal2 = new scrollReveal({elem: document.getElementById('srcontainer')});
</script>	
